package 超市会员管理系统;

import java.lang.reflect.Member;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

public class YeWu {
    List<HuiYuan> list=new ArrayList();
    Scanner input=new Scanner(System.in);
    //输入欢迎菜单
    public void caidan(){
        System.out.println("******欢迎使用搜搜移动义务大厅******");
        System.out.println("1，用户登录    2，用户注册  3使用搜搜   4，话费充值  5，资费说明    6，退出");
        System.out.println("请选择");
    }

    //搭建项目整体流程
    public void start(){
        do {
            caidan();
            int chose=input.nextInt();
            switch (chose){
                case 1:
                    register();
                    continue;
                case 2:
                    if (xiugaimm()){
                        System.out.println("新密码设置成功");
                    }
                    continue;
                case 3:
                    if (leijijifen()){
                        System.out.println("积分累计成功");
                    }
                    continue;
                case 4:
                    if (duihuan()){
                        System.out.println("积分兑换成功");
                    }

                    continue;
                case 5:
                    chaxun();
                    continue;
                case 6:
                    System.out.println("感谢您的使用，欢迎下次使用！");
                    break;
                    default:
                        System.out.println("您的操作有误，请重新选择：");
                        continue;
            }
            break;
        }while (true);
    }
    public int createId(){
        Random random=new Random();
        int id=random.nextInt(999999999);
        //保证生成的会员卡号不能和之前已有的会员卡号重复
        for (HuiYuan h:list){
            if (h.getKahao()==id){
                id=random.nextInt(999999999);
            }
        }
        return id;
    }
    public void register(){
        HuiYuan hy=new HuiYuan();
        System.out.print("请输入手机卡号: ");
        hy.setAnme(input.next());
        System.out.print("请输入密码: ");
        //会员卡的密码不能低于6位
        String pwd;
        boolean flag=true;
        do {
            pwd=input.next();
            if (pwd.length()<6){
                System.out.println("会员密码不能小于六位，请冲洗输入注册密码");
                flag=false;
            }else {
                flag=true;
                hy.setMima(pwd);
            }
        }while (!flag);
        hy.setKahao(createId());
        hy.setJifen(100);
        list.add(hy);
        System.out.println("恭喜开通会员卡号，系统赠送100积分,会员卡号："+hy.getKahao());
    }
    //验证会员是否存在
    //查询条件（会员卡号和密码）--封装在参数m中传过来
    public HuiYuan has(HuiYuan m){
       HuiYuan huiyuan =null;
       for (HuiYuan h:list){
           if (m.getKahao()==h.getKahao()&&m.getMima().equals(h.getMima())){
               //会员存在
               huiyuan=h;
           }
       }

       return huiyuan;
    }
    //修改密码
    public boolean xiugaimm(){
        boolean falg=true;
        HuiYuan huiyuan=new HuiYuan();//密码修改是否成功
        System.out.println("输入会员卡号");
        huiyuan.setKahao(input.nextInt());
        System.out.println("输入会员密码");
        huiyuan.setMima(input.next());
        huiyuan=has(huiyuan);
        if (huiyuan!=null){
            System.out.println("请输入新会员密码");
            String pwd;
            boolean falgnew=true;
            do {
                pwd=input.next();
                if (pwd.length()<6){
                    System.out.println("会员密码不能小于六位，请重新输入注册密码");
                    falgnew=false;
                }else{
                    huiyuan.setMima(pwd);
                    falgnew=true;
                    falg=true;
                }
            }while (!falgnew);
        }else {
            System.out.println("输入的账号或密码错误，无法修改密码！");
            falg=false;
        }
        return falg;
    }
    //积分累计
    public boolean leijijifen(){
        boolean flag=true;
        HuiYuan huiyuan=new HuiYuan();
        System.out.println("输入会员卡号");
        huiyuan.setKahao(input.nextInt());
        System.out.println("输入会员密码");
        huiyuan.setMima(input.next());
        huiyuan=has(huiyuan);
        if (huiyuan!=null){
            System.out.println("请输入此次消费金额（消费1元1积分）");
            int score=input.nextInt();
            huiyuan.setJifen(huiyuan.getJifen()+score);
            flag=true;
        }else {
            System.out.println("输入的会员卡号或密码错误，不能累计积分");
            flag=false;
        }
        return flag;
    }
    //查询剩余积分
    public void chaxun(){
        HuiYuan huiyuan=new HuiYuan();
        System.out.println("输入会员卡号");
        huiyuan.setKahao(input.nextInt());
        System.out.println("输入会员密码");
        huiyuan.setMima(input.next());
        huiyuan=has(huiyuan);
        if (huiyuan!=null){
            System.out.println("姓名\t会员卡号\t剩余积分\t");
            System.out.println(huiyuan.getAnme()+"\t"+huiyuan.getKahao()+"\t"+huiyuan.getJifen());
        }else{
            System.out.println("输入的密码或者卡号错误，无法查询");
        }
    }
    //积分兑换
    public boolean duihuan(){
        boolean flag=true;
        HuiYuan huiyuan=new HuiYuan();
        System.out.println("输入会员卡号");
        huiyuan.setKahao(input.nextInt());
        System.out.println("输入会员密码");
        huiyuan.setMima(input.next());
        huiyuan=has(huiyuan);
        if (huiyuan!=null){
            System.out.println("请输入兑换的积分（100积分抵用0.1元，不足100的积分不能抵用）");
            int score=input.nextInt();
            if (score<=huiyuan.getJifen()){
                huiyuan.setJifen(huiyuan.getJifen()-score);
                System.out.println("会员积分抵扣"+score/100+"元");
                flag=true;
            }else {
                System.out.println("积分不足！");
                flag=false;
            }
        }else {
            System.out.println("输入的密码或卡号错误，无法兑换积分");
            flag=false;
        }
        return flag;
    }

}
